package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.vo.CategoryView;
import com.atguigu.gmall.product.entity.SkuImage;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName SKuRpcController
 * @Description TODO
 * @Author king
 * @Date 2022/11/7 18:27
 * @Version 1.0
 */
@RequestMapping("/api/inner/rpc/product")
@RestController
public class SKuRpcController {

    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SkuImageService skuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @Autowired
    BaseCategory1Service baseCategory1Service;

    @GetMapping("/skuinfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId){

        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return Result.ok(skuInfo);
    }

    @GetMapping("/skuinfo/images/{skuId}")
    public Result<List<SkuImage>> getSkuImages(@PathVariable("skuId")Long skuId){
        QueryWrapper<SkuImage> wrapper = new QueryWrapper<>();
        wrapper.eq("sku_id", skuId);

        List<SkuImage> skuImages = skuImageService.list(wrapper);
        return Result.ok(skuImages);
    }

    @GetMapping("/skuinfo/spu/saleattrandvalue/{spuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttr(@PathVariable("spuId") Long spuId, @RequestParam("skuId") Long skuId){

        List<SpuSaleAttr> spuSaleAttrs = spuSaleAttrService.getSpuSaleAttrAndValue(spuId,skuId);
        return Result.ok(spuSaleAttrs);
    }

    @GetMapping("/skuinfo/categoryview/{c3Id}")
    public Result<CategoryView> getSkuCategoryView(@PathVariable("c3Id")Long c3Id){
        CategoryView categoryView = baseCategory1Service.getSkuCategoryView(c3Id);
        return Result.ok(categoryView);
    }

    @GetMapping("skuinfo/valuejson/{spuId}")
    public Result<String> getSkuValueJson(@PathVariable("spuId")Long spuId){

        String json = spuSaleAttrService.getSkuValueJson(spuId);
        return Result.ok(json);
    }

    @GetMapping("skuinfo/price/{skuId}")
    public Result<BigDecimal> getSkuPrice(@PathVariable("skuId")Long skuId){

        BigDecimal price = skuInfoService.getSkuRealTimePrice(skuId);
        return Result.ok(price);
    }

    @GetMapping("/hehe")
    public Result haha(){
        return Result.ok();
    }
}
